package com.tiger.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiger.utils.RedisUtils;
import com.tiger.mapper.base.InterfaceMapper;
import com.tiger.pojo.base.Interface;
import com.tiger.service.transactional.InterfaceTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author HuXuehao (StudiousTiger)
 * @desc 这个类的作用是 对项目的接口的一些操作
 * @date 2022/6/25
 */
@RestController
public class InterfaceController {
    @Autowired
    private InterfaceTransactional transactional;
    @Autowired
    private InterfaceMapper interfaceMapper;
    @Autowired
    private RedisUtils redisUtils;
    /**
     * 获取页数
     * @return
     */
    @RequestMapping(value = "/interface/pages", name = "获取接口信息分页总页数/admin")
    public Long getPages(){
        try {
            Page<Interface> page = new Page<>(1, 20);
            Page<Interface> interfacePage = interfaceMapper.selectPage(page, null);
            return interfacePage.getPages();
        } catch (Exception e) {
            return 0L;
        }
    }

    /**
     * 获取接口列表
     * @return
     */
    @RequestMapping(value = "/interface/list/{currentPage}", name = "获取接口列表/admin")
    public String getList(@PathVariable int currentPage){
        try {
            Page<Interface> page = new Page<>(currentPage, 20);
            QueryWrapper<Interface> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("role");
            wrapper.orderByAsc("url");
            Page<Interface> interfacePage = interfaceMapper.selectPage(page, wrapper);
            List<Interface> res = interfacePage.getRecords();
            if (res.size()>0){
                return JSON.toJSONString(res);
            } else {
                return "empty";
            }
        } catch (Exception e) {
            return "empty";
        }
    }

    /**
     * 同步接口信息
     * @return
     */
    @RequestMapping(value = "/interface/synchronization", name = "同步接口信息/admin")
    public String synchronizationInterface(){
        try {
            transactional.synchronizationInterface();
        } catch (Exception e) {
            return "false";
        }
        return "true";
    }

    /**
     * 更新接口信息
     * @param interfaceInfo
     * @return
     */
    @RequestMapping(value = "/interface/update/{interfaceInfo}", name = "更新接口信息/admin")
    public String updateInterfaceInfo(@PathVariable String interfaceInfo){
        interfaceInfo = interfaceInfo.replaceAll("-", "/");
        try {
            Interface anInterface = JSON.parseObject(interfaceInfo, Interface.class);
            int i = interfaceMapper.updateById(anInterface);
            // 同步缓存
            if (i == 1) {
                String[] arr = new String[2];
                arr[0] = anInterface.getRole();
                arr[1] = anInterface.getDisableTag()+"";
                redisUtils.hset("interfaceInfo",anInterface.getUrl(), JSON.toJSONString(arr));
            }
        } catch (Exception e) {
            return "false";
        }
        return "true";
    }


    /**
     * 添加接口
     * @param interfaceInfo
     * @return
     */
    @RequestMapping(value = "/interface/add/{interfaceInfo}", name = "添加接口/admin")
    public String addInterface(@PathVariable String interfaceInfo){
        interfaceInfo = interfaceInfo.replaceAll("-", "/");
        try {
            Interface anInterface = JSON.parseObject(interfaceInfo, Interface.class);
            int insert = interfaceMapper.insert(anInterface);
            // 同步缓存
            if (insert == 1) {
                String[] arr = new String[2];
                arr[0] = anInterface.getRole();
                arr[1] = anInterface.getDisableTag()+"";
                redisUtils.hset("interfaceInfo",anInterface.getUrl(), JSON.toJSONString(arr));
            }
        } catch (Exception e) {
            return "false";
        }
        return "true";
    }

    /**
     * 删除接口
     * @param interfaceInfo
     * @return
     */
    @RequestMapping(value = "/interface/delete/{interfaceInfo}", name = "删除接口/admin")
    public String deleteInterface(@PathVariable String interfaceInfo){
        interfaceInfo = interfaceInfo.replaceAll("-", "/");
        Interface anInterface = JSON.parseObject(interfaceInfo, Interface.class);
        try {
            int i = interfaceMapper.deleteById(anInterface.getId());
            // 同步缓存
            if (i == 1) {
                redisUtils.hdel("interfaceInfo",anInterface.getUrl());
            }
        } catch (Exception e) {
            return "false";
        }
        return "true";
    }

    /**
     * 重置接口
     * @return
     */
    @RequestMapping(value = "/interface/reset", name = "重置接口信息/admin")
    public String resetInterface(){
        try {
            transactional.resetInterface();
        } catch (Exception e) {
            return "false";
        }
        return "true";
    }
    /**
     * 搜索接口
     * @return
     */
    @RequestMapping(value = "/interface/search/{name}/{url}/{role}/{disable}", name = "接口管理-搜索/admin")
    public String search(@PathVariable String name, @PathVariable String url, @PathVariable String role, @PathVariable String disable){
        try {
            if (!"null".equals(url)) {
                url = url.replaceAll("-", "/");
            } else {
                url = "";
            }
            if ("null".equals(name)){
                name = "";
            }
            if ("null".equals(role)){
                role = "";
            }
            if ("null".equals(disable)){
                disable = "";
            } else {
                if (disable.equals("可用")) {
                    disable = "0";
                } else {
                    disable = "1";
                }
            }
            QueryWrapper<Interface> wrapper = new QueryWrapper<>();
            wrapper.like("name",name);
            wrapper.like("url",url);
            wrapper.like("role",role);
            wrapper.like("disable_tag",disable);
            wrapper.orderByDesc("role");
            wrapper.orderByAsc("url");
            List<Interface> res = interfaceMapper.selectList(wrapper);
            if (res.size()>0){
                return JSON.toJSONString(res);
            } else {
                return "empty";
            }
        } catch (Exception e) {
            return "empty";
        }
    }
}
